package sunwul.hospital.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import sunwul.hospital.common.result.Result;
import sunwul.hospital.common.utils.MD5;
import sunwul.hospital.hosp.service.HospitalSetService;
import sunwul.hospital.model.model.hosp.HospitalSet;
import sunwul.hospital.model.vo.hosp.HospitalSetQueryVo;

import java.util.List;
import java.util.Random;

/**
 * @author sunwul
 * @date 2023/10/22 18:38:22
 * @description 医院设置表 HospitalSet
 */
@Api(tags = "医院设置管理")
@CrossOrigin
@RestController
@RequestMapping("admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("/findAll")
    public Result<List<HospitalSet>> findAllHospitalSet() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }


    @ApiOperation(value = "根据ID逻辑删除医院设置")
    @ApiParam(name = "id", value = "医院设置表ID", required = true)
    @DeleteMapping("{id}")
    public Result removeHospitalSetById(@PathVariable Long id) {
//        boolean flag = hospitalSetService.removeById(id);
//        if (!flag) {
//            return Result.fail();
//        }
//        return Result.ok();
        return hospitalSetService.removeById(id) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "条件与分页查询医院设置")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页", paramType = "path", dataType = "Long", required = true),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "path", dataType = "Long", required = true)})
    @PostMapping("/findPage/{current}/{limit}")
    public Result findPageHospitalSet(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
//        Page<HospitalSet> page = new Page<>(current, limit);
//        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<HospitalSet>();
//        //条件不为空时添加查询条件
//        if (!StringUtils.isEmpty(hospitalSetQueryVo.getHosname())) {
//            wrapper.like("hosname", hospitalSetQueryVo.getHosname());
//        }
//        if (!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode())){
//            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
//        }
//        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page,wrapper);
        Page<HospitalSet> hospitalSetPage = hospitalSetService.query()
                .eq(!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode()), "hoscode", hospitalSetQueryVo.getHoscode())
                .like(!StringUtils.isEmpty(hospitalSetQueryVo.getHosname()), "hosname", hospitalSetQueryVo.getHosname())
                .page(new Page<>(current, limit));
        return Result.ok(hospitalSetPage);
    }

    @ApiOperation(value = "添加医院设置")
    @PostMapping("/save")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        // 设置状态 1-使用 0-不使用
        hospitalSet.setStatus(1);
        // 设置签名密钥
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + new Random().nextInt(1000)));
        return hospitalSetService.save(hospitalSet) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "根据ID获取医院设置")
    @GetMapping("getById/{id}")
    public Result getHospitalSetById(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    @ApiOperation(value = "修改医院设置")
    @PostMapping("update")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
//        boolean flag = hospitalSetService.updateById(hospitalSet);
//        if(flag) {
//            return Result.ok();
//        } else {
//            return Result.fail();
//        }
        return hospitalSetService.updateById(hospitalSet) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        return hospitalSetService.removeByIds(idList) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "医院设置锁定与解锁")
    @PutMapping("lock/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        // 更新
        return hospitalSetService.updateById(hospitalSet) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "发送密钥")
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }
}
